#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置按钮的图标
    ui->pushButton_target->setIcon(QIcon(":/image/doge.jpg"));
    ui->pushButton_target->setIconSize(QSize(100, 100));
    ui->pushButton_up->setIcon(QIcon(":/image/up.jpg"));
    ui->pushButton_down->setIcon(QIcon(":/image/down.jpg"));
    ui->pushButton_left->setIcon(QIcon(":/image/left.jpg"));
    ui->pushButton_right->setIcon(QIcon(":/image/right.jpg"));

    // 设置快捷键
    // 直接通过按键的名字来设置，虽然简单，但是容易写错
    ui->pushButton_up->setShortcut(QKeySequence("w"));
    ui->pushButton_down->setShortcut(QKeySequence("s"));
    ui->pushButton_left->setShortcut(QKeySequence("ctrl+a"));  // 不能带空格
    ui->pushButton_right->setShortcut(QKeySequence("ctrl+d"));

    // 通过按键的枚举来设置快捷键，写错了编译就会报错
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_Up));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_Down));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));

    // 开启鼠标点击的连发功能（键盘的连发默认就是支持的）
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    // 获取到target的位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次的位置，设置新的位置
    ui->pushButton_target->move(rect.x(), rect.y() - 5);
}

void Widget::on_pushButton_down_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->move(rect.x(), rect.y() + 5);
}

void Widget::on_pushButton_left_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->move(rect.x() - 5, rect.y());
}

void Widget::on_pushButton_right_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->move(rect.x() + 5, rect.y());
}
